package cn.school.servlet;

import cn.school.entity.Student;
import cn.school.util.StringUtil;
import org.apache.commons.fileupload.FileItem;
import org.apache.commons.fileupload.FileUploadException;
import org.apache.commons.fileupload.disk.DiskFileItemFactory;
import org.apache.commons.fileupload.servlet.ServletFileUpload;

import javax.servlet.ServletException;
import javax.servlet.ServletInputStream;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.File;
import java.io.IOException;
import java.io.PrintWriter;
import java.util.List;
import java.util.UUID;

@WebServlet(name = "ToAddStudentServlet",value = {"/toAddStudentServlet","/addStudentServlet"})
public class ToAddStudentServlet extends HttpServlet {
    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        request.setCharacterEncoding("utf-8");
        response.setContentType("text/html;charset=utf-8");
        PrintWriter out = response.getWriter();
        DiskFileItemFactory factory=new DiskFileItemFactory();
        ServletFileUpload upload=new ServletFileUpload(factory);
        upload.setHeaderEncoding("utf-8");
        Student stu=new Student();
        try {
            List<FileItem> fileItems = upload.parseRequest(request);
            for(FileItem fi:fileItems){
                if(fi.isFormField()){
                    //普通的表单域
                    out.print("<h1>名字是："+fi.getFieldName()+",值是"+fi.getString("utf-8")+"</h1>");
                    switch (fi.getFieldName()){
                        case "sname":
                            stu.setSname(fi.getString("utf-8"));
                            break;
                        case "sex":
                            stu.setSex(fi.getString("utf-8"));
                            break;
                        case "gradeid":
                            stu.setGradeid(StringUtil.toLong(fi.getString("utf-8")));
                            break;
                    }
                }else{
                    //文件域
                    if(fi.getSize()<1)
                        continue;
                    out.print("<h1>文件域的名字是："+fi.getFieldName()+",文件名是："+fi.getName()+"</h1>");
                    String picpath=request.getServletContext().getRealPath("images");
                    File file=new File(picpath);
                    if(!file.exists())
                        file.mkdirs();
                    //1:改名字,保留后缀
                    //2:文件的路径，需要保存到数据库中。。。
                    String suffix=fi.getName().substring(fi.getName().lastIndexOf('.'));
                    if(!(suffix.equals(".jpg")||suffix.equals(".png"))){
                        //throw new
                    }
                    String newName= UUID.randomUUID()+suffix;
                    fi.write(new File(file,newName));
                    out.print("<img src='images/"+newName+"' height='399px' width='400px'/>");
                }

            }
        } catch (Exception e) {
            e.printStackTrace();
        }

//        PrintWriter out = response.getWriter();
//        ServletInputStream in = request.getInputStream();
//        int ch=0;
//        while ((ch=in.read())>-1){
//            out.print((char)ch);
//        }
        //
//        out.print("<h1>"+request.getParameter("sname")+"</h1>");
//        out.print("<h1>"+request.getParameter("qq")+"</h1>");
//        out.print("<h1>"+request.getParameter("wechat")+"</h1>");
//        out.print("<h1>"+request.getParameter("idcard")+"</h1>");
    }

    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        request.getRequestDispatcher("/WEB-INF/jsp/student/addStudent.jsp").forward(request,response);
    }
}
